// firstPackage/pages/searchPage/searchPage.js

import {
  queryHouseCommentList,
  queryHousePeopleList,

  queryNewHouseByCondition,
} from "../../../service/house_service"
import {
  queryRecommendSearchWords
} from "../../../service/jb_home_service"
import debounce from "../../../tools/debounce"
const HISTORYHK = "historyHTag";
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    historySearchList: [],//历史记录
    storyKey: HISTORYHK,
    requestData: {
      cityName: "",
      houseStyle: "",
      maxArea: null,
      maxTotalPrice: null,
      maxUnitPrice: null,
      minArea: null,
      minTotalPrice: null,
      minUnitPrice: null,
      page: 0,
      pageSize: 10,
      saleStatus: "",
      subway: "",
      tags: [],
      words: "",
      zone: ""
    },
    reList: [

    ],
    tabArr: [
      {
        title: "销量榜",
        key: "销量榜"
      },
      {
        title: "人气榜",
        key: "人气榜"
      },
      {
        title: "热议榜",
        key: "热议榜"
      },
    ],
    selectIndex: 0,
    rankList: [

    ],
    searchWord: "",//搜索词
    searchShowList: [],//搜索显示结果,
    searchList: [],//搜索处理列表
    cityName: "成都市",
    page: 0,
    pageSize: 10,
    isMap: "",//是否从地图找房过来的

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isMap == 1) {
      this.setData({
        isMap: options.isMap
      })
    }

    this.getSearchHistoryList();
    //JJ-TODO
    this.queryRecommendSearchWords();
  },




  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //获取搜索的历史记录:
  getSearchHistoryList() {
    wx.getStorage({
      key: this.data.storyKey,
      success: res => {
        console.log("JJLog:读取到的数据是:" + JSON.stringify(res));
        this.setData({
          historySearchList: res.data
        })
      },
      fail: err => {
        console.log("JJLog:读取历史记录失败:" + JSON.stringify(err));
        this.setData({
          historySearchList: []
        })
      }
    })

  },

  //搜索回调
  searchAction(e) {
    // console.log("JJLog:搜索回调:" + JSON.stringify(e));
    this.setData({
      searchWord: e.detail.searchWord,
    })
    // debounce(this.queryNewHouseByCondition);
    if (!e.detail.searchWord) {
      wx.showToast({
        title: '请输入搜索内容',
        icon: "none"
      })
      return;
    }
    this.saveHistoryList(this.data.searchWord);
    wx.navigateTo({
      url: "/featurePackage/pages/searchResult/searchResult?searchWord=" + e.detail.searchWord
    })
  },








  //搜索房源:
  queryNewHouseByCondition() {
    wx.showLoading();
    queryNewHouseByCondition(this.data.requestData).then(res => {
      wx.hideLoading();
      console.log("JJLog:搜索结果:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          searchShowList: [],
        })
        if (res.data && res.data.length > 0) {
          if (this.data.searchWord) {
            this.setData({
              searchShowList: res.data
            })
          }
          let searchData = res.data.map((res) => {
            return {
              key: this.data.searchWord,
              name: res.name
            }
          })
          this.setData({
            searchList: searchData
          })
          this.saveHistoryList(this.data.searchWord);
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "搜索失败",
        icon: "none"
      })
      console.error("JJLog:queryNewHouseByCondition", err);
    })
  },
  //存储搜索历史:
  saveHistoryList(searchWord) {
    let arr = this.data.historySearchList || [];
    if (arr.length > 0) {
      let takeRepate = arr.find((item) => {
        return item == searchWord
      })
      if (takeRepate) return;
    }
    if (!searchWord) {
      return;
    }
    arr.push(searchWord);
    this.setData({
      historySearchList: arr
    })
    wx.setStorage({
      data: this.data.historySearchList,
      key: this.data.storyKey,
    })

  },
  //删除历史搜索记录:
  cancelHistoryList() {
    this.setData({
      historySearchList: []
    })
    wx.removeStorage({
      key: this.data.storyKey,
    })
  },


  //点击历史tag进行搜索:
  clickHistoryTag(e) {
    let item = e.currentTarget.dataset.item;
    this.setData({
      searchWord: item,

    })
    wx.navigateTo({
      url: "/featurePackage/pages/searchResult/searchResult?searchWord=" + this.data.searchWord
    })


  },
  //点击推荐tag搜索跳转到搜索结果页
  recommendTagClick(e) {
    let item = e.currentTarget.dataset.item;
    // this.setData({
    //   searchWord: item,
    //   'requestData.words': item,
    // })
    // this.queryNewHouseByCondition();
    wx.navigateTo({
      url: "/featurePackage/pages/searchResult/searchResult?searchWord=" + item
    })
  },


  /**
   * 推荐的搜索标签
   */
  queryRecommendSearchWords() {
    queryRecommendSearchWords().then(res => {
      console.log("JJLog:推荐搜索标签是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          reList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:queryRecommendSearchWords", err);
    })
  },



  //跳转到房源详情;
  pushToHouseDetail(e) {
    let houseNumber = e.currentTarget.dataset.number;
    wx.navigateTo({
      url: '/newHousePackage/pages/newHouseDetail/newHouseDetail?houseNumber=' + houseNumber,
    })
  },



})